from django import forms
from django.contrib.auth.models import User
from usuarios.models import *



class CreateForm(forms.ModelForm):
    password1 = forms.CharField(max_length=10, widget= forms.PasswordInput)
    password2 = forms.CharField(max_length=10, widget= forms.PasswordInput)
    first_name = forms.CharField(max_length=30, required = True )
    last_name = forms.CharField(max_length=30, required = True )
    email = forms.EmailField(max_length=75,required=True)
    def clean_password1(self):
        password1 = self.cleaned_data.get('password1', '')
        num_words = len(password1)
        if num_words < 2:
            raise forms.ValidationError('Password debil')
        return password1
                
    def clean_password2(self):
        password1 = self.cleaned_data.get('password1', '')
        password2 = self.cleaned_data.get('password2', '')
        if password1 != password2:
            raise forms.ValidationError('Repita el password correctamente')        
        return password2
        
    class Meta:
        model = User
        fields = ('username','first_name', 'last_name', 'email')


        
class EditForm(forms.ModelForm):
    first_name = forms.CharField(max_length=30, required = True )
    last_name = forms.CharField(max_length=30, required = True )
    email = forms.EmailField(max_length=75,required=True)
    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email')

class PermisosForm(forms.ModelForm):
    supervisor=forms.BooleanField(required=False)
    experto=forms.BooleanField(required=False)
    class Meta:
        model = UserPermisos
        fields = ('supervisor', 'experto')
        
class CambiarPassForm(forms.Form):
    passwordviejo=forms.CharField(max_length=10, widget= forms.PasswordInput)
    password1 = forms.CharField(max_length=10, widget= forms.PasswordInput)
    password2 = forms.CharField(max_length=10, widget= forms.PasswordInput)
    
    def clean_password1(self):
        password1 = self.cleaned_data.get('password1', '')
        num_words = len(password1)
        if num_words < 2:
            raise forms.ValidationError('Password debil')
        return password1
    
    def clean_password2(self):
        password1 = self.cleaned_data.get('password1', '')
        password2 = self.cleaned_data.get('password2', '')
        if password1 != password2:
            raise forms.ValidationError('Repita el password correctamente')        
        return password2
    